package ModelLayer;

import java.util.*;
import java.text.*;
public class Order
{
    private int id;
    private String date;
    private int employeeId;
    private boolean ready;
    private int totalProducts;
    private HashMap<Integer, Integer> products;
    private SaleContainer saleContainer;
    private ProductContainer productContainer;

    public Order(int id, int employeeId){
        this.id = id;
        this.employeeId = employeeId;
        date = getOrderDate();
        ready = false;
        products = new HashMap<Integer, Integer>();
    }

    private String getOrderDate(){
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy  HH:mm:ss");
        Date date = new Date();
        String dateString = dateFormat.format(date);
        return dateString;
    }

    public void addProductInOrderAutomatically(int saleId){
        Sale sObject = saleContainer.getInstance().findSaleById(saleId);
        int productBarcode = 0;
        int amount = 0;
        for(int key : sObject.getProducts().keySet()){
            Product pObject = productContainer.getInstance().findProductByBarcode(key);
            if(pObject.getAmount() < pObject.getMinAmount()){
                productBarcode = key;
                amount = (pObject.getMaxAmount() - pObject.getAmount()) - pObject.getOrderAmount();
                products.put(productBarcode, amount);
                pObject.recalculateOrderAmount(amount);
                totalProducts = totalProducts + amount;
            }
        }
    }

    public void addProductInOrderManually(int productBarcode, int amount){
        if(products.containsKey(productBarcode)) {
            int modification = amount;
            modification += products.get(productBarcode);
            products.remove(productBarcode);
            products.put(productBarcode,modification);
            ProductContainer.getInstance().findProductByBarcode(productBarcode).setOrdered(amount);
            EmployeeContainer.getInstance().findEmployeeById(employeeId).setOrdered(amount);
        } else {
            products.put(productBarcode, amount);
            ProductContainer.getInstance().findProductByBarcode(productBarcode).setOrdered(amount);
            EmployeeContainer.getInstance().findEmployeeById(employeeId).setOrdered(amount);
        }
    }

    public void removeProductFromOrderManually(int productBarcode, int amount){
        if(products.containsKey(productBarcode) && products.get(productBarcode) > amount){
            int modification = products.get(productBarcode) - amount;
            products.remove(productBarcode);
            products.put(productBarcode, modification);
            ProductContainer.getInstance().findProductByBarcode(productBarcode).setOrdered(-amount);
            EmployeeContainer.getInstance().findEmployeeById(employeeId).setOrdered(-amount);
        }
    }

    public boolean isReady(){
        return ready;
    }

    public void setReady() {
        if(totalProducts == 0) {
            ready = false;
        } else {
            ready = true;
        }
    }

    public int getId(){
        return id;
    }

    public void setId(int id){
        this.id = id;
    }

    public String getDate(){
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public int getEmployeeID() {
        return employeeId;
    }

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }

    public HashMap<Integer,Integer> getProducts(){
        return products;
    }
}
